package com.xqy.wexmini.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.xqy.wexmini.constant.Constant;
import com.xqy.wexmini.domain.Vegetable;
import com.xqy.wexmini.mapper.VegetableMapper;
import com.xqy.wexmini.service.ElasticService;
import com.xqy.wexmini.utils.ElasticsearchUtil;
import com.xqy.wexmini.utils.baiduUtils;
import com.xqy.wexmini.utils.oss.Fileload;
import com.xqy.wexmini.utils.oss.UploadUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.aspectj.util.FileUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.*;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.List;
import java.util.Map;
import java.util.UUID;

@Api(tags = "蔬菜信息")
@RestController
public class VegetableController {
    @Autowired
    private VegetableMapper vegetableMapper;
    @Autowired
    private ElasticsearchUtil elasticsearchUtil;

    @ApiOperation("查询所有蔬菜信息")
    @PostMapping("/selectAll")
    public List<Vegetable> getAll(){
        List<Vegetable> list = vegetableMapper.selectList(null);
        //List<Vegetable> list = vegetableMapper.listAll();
        return list;
    }


    @ApiOperation("搜索蔬菜")
    @PostMapping("/selectVe")
    public List<Vegetable> getVe(String name) throws IOException {
        List<Vegetable> vegetables = vegetableMapper.selectList(new LambdaQueryWrapper<Vegetable>().like(Vegetable::getName,name));
        return vegetables;
    }

    @ApiOperation("图片上传")
    @PostMapping("/load")
    public String load(@RequestBody MultipartFile multipartFile){
        UploadUtils uploadUtils=new UploadUtils();
        try {
            return uploadUtils.asd(multipartFile);
        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    }

    @ApiOperation("根据图片url返回蔬菜对象")
    @PostMapping("/selectUrl")
    public Vegetable selectUrl(String url){
        Vegetable vegetable = vegetableMapper.selectOne(new LambdaQueryWrapper<Vegetable>().eq(Vegetable::getPicture, url));
        return vegetable;
    }

    //将图片url转换成file对象，file对象再转换成二进制流
    @ApiOperation("扫描图片")
    @PostMapping("/scan")
    public String scan(@RequestBody MultipartFile multipartFile) throws Exception {
        UploadUtils uploadUtils=new UploadUtils();
        String s = null;
        try {
            uploadUtils.lingshi(multipartFile);
        } catch (IOException e) {
            e.printStackTrace();
        }
        String destUrl = "https://vegetable666.oss-cn-shenzhen.aliyuncs.com/temporary";

        HttpURLConnection httpUrl = (HttpURLConnection) new URL(destUrl).openConnection();
        httpUrl.connect();
        File file = inputStreamToFile(httpUrl.getInputStream(),"xia");
        //return baiduUtils.ingredient("/tupian/R.jpg");
        return baiduUtils.ingredient(file);
    }

/*    @ApiOperation("扫描图片的信息")
    @PostMapping("/aa")
    public String scan1() throws Exception {
        String destUrl = "https://vegetable666.oss-cn-shenzhen.aliyuncs.com/temporary";

        HttpURLConnection httpUrl = (HttpURLConnection) new URL(destUrl).openConnection();
        httpUrl.connect();
        File file = inputStreamToFile(httpUrl.getInputStream(),"xia");
        //return baiduUtils.ingredient("/tupian/R.jpg");
        return baiduUtils.ingredient(file);
    }*/
    public static File inputStreamToFile(InputStream ins, String name) throws Exception{
        File file = new File(System.getProperty("java.io.tmpdir") + File.separator + name);//System.getProperty("java.io.tmpdir")缓存的名字
        if (file.exists()) {
            file.delete();//如果缓存中存在该文件就删除
        }
        OutputStream os = new FileOutputStream(file);
        int bytesRead;
        int len = 8192;
        byte[] buffer = new byte[len];
        while ((bytesRead = ins.read(buffer, 0, len)) != -1) {
            os.write(buffer, 0, bytesRead);
        }
        os.close();
        ins.close();
        return file;
    }

    @ApiOperation("扫描图片2")
    @PostMapping("/scan1")
    public Vegetable scan1(@RequestBody MultipartFile multipartFile) throws IOException {
        String s = baiduUtils.ingredient(multipartFile);
        JSONObject jsonObject = JSON.parseObject(s);
        List<JSONObject> list = (List<JSONObject>) jsonObject.get("result");
        JSONObject js=list.get(0);
        String name = (String)js.get("name");
        Vegetable vegetable = elasticsearchUtil.selectOne(name, Constant.VEGETABLE);
        return vegetable;
    }
}
